package com.aspirecn.order.controller;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.List;

import javax.validation.Valid;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import com.aspirecn.order.common.RestApiResponseHandler;
import com.aspirecn.order.domain.RequestMsg;
import com.aspirecn.order.domain.ResponseHead;
import com.aspirecn.order.domain.ResponseMsg;
import com.aspirecn.order.domain.ResultBean;
import com.aspirecn.order.domain.vo.QueryOrderInfoByIdInVo;
import com.aspirecn.order.domain.vo.QueryOrderInfoByIdOutVo;
import com.aspirecn.order.service.OrderService;

/**
 * @author chenyuanli
 */
@RestController
public class OrderController {

	Logger logger = LoggerFactory.getLogger(OrderController.class);

	@Autowired
	private OrderService orderService;
	
	@RequestMapping(value = "/queryOrderInfoById", method = RequestMethod.POST, consumes = MediaType.APPLICATION_JSON_VALUE, produces = MediaType.APPLICATION_JSON_VALUE)
	public ResponseEntity<ResponseMsg<QueryOrderInfoByIdOutVo>> queryOrderInfoById(@Valid @RequestBody RequestMsg<QueryOrderInfoByIdInVo> msg, BindingResult errors) {
		ResponseMsg response = new ResponseMsg<List<QueryOrderInfoByIdOutVo>>();
		ResponseHead rspHead = new ResponseHead();
		rspHead.setRequestId(msg.getHead().getRequestId());
		response.setHead(rspHead);

		ResultBean<QueryOrderInfoByIdOutVo> result = orderService.queryOrderInfoById(msg.getData().getId());
		String info = result.getMsg();
        if (result.getCode() != ResultBean.SUCCESS) {
        	response = RestApiResponseHandler.errorResponseMsg(rspHead, info);
        } else {
        	response = RestApiResponseHandler.successResponseMsg(rspHead, info);
            response.setData(result.getData());
        }
		return new ResponseEntity<>(response, HttpStatus.OK);
	}
	
	
	@RequestMapping(value = "/queryOrderInfoByIdNoDisco", method = RequestMethod.POST, consumes = MediaType.APPLICATION_JSON_VALUE, produces = MediaType.APPLICATION_JSON_VALUE)
	public ResponseEntity<ResponseMsg<QueryOrderInfoByIdOutVo>> queryOrderInfoByIdNoDisco(@Valid @RequestBody RequestMsg<QueryOrderInfoByIdInVo> msg, BindingResult errors) {
		ResponseMsg response = new ResponseMsg<List<QueryOrderInfoByIdOutVo>>();
		ResponseHead rspHead = new ResponseHead();
		rspHead.setRequestId(msg.getHead().getRequestId());
		response.setHead(rspHead);

		ResultBean<QueryOrderInfoByIdOutVo> result = orderService.queryOrderInfoByIdNoDisco(msg.getData().getId());
		String info = result.getMsg();
        if (result.getCode() != ResultBean.SUCCESS) {
        	response = RestApiResponseHandler.errorResponseMsg(rspHead, info);
        } else {
        	response = RestApiResponseHandler.successResponseMsg(rspHead, info);
            response.setData(result.getData());
        }
		return new ResponseEntity<>(response, HttpStatus.OK);
	}
	@RequestMapping(value = "/testget", method = RequestMethod.GET)
	@PreAuthorize("hasAuthority('query-demo')")
	public String testget() {
		return "ok";
	}
}
